package com.aaa.controller;

import com.aaa.dao.IBookDao;
import com.aaa.dao.impl.IBookDaoImpl;
import com.aaa.entity.Book;
import com.aaa.utils.LayTab;
import com.alibaba.fastjson.JSON;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

@WebServlet("/book/*")
public class BookController extends HttpServlet {
    private IBookDao bookDao = new IBookDaoImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String uri = req.getRequestURI();
        String[] split = uri.split("/");
        String s = split[split.length - 1];
        switch (s){
            case "findAllBook":
                findAllBook(req,resp);
                break;
            case "updateStatus":
                updateStatus(req,resp);
                break;
            case"delBookById":
                delBookById(req,resp);
                break;
            case"addOrUpdateBook":
                addOrUpdateBook(req,resp);
                break;
        }

    }

    /**
     * 新增和修改书籍的操作
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void addOrUpdateBook(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.接收前台传递来的所有的参数
        Map<String, String[]> map = req.getParameterMap();
        //2.将获取到的前台发来的所有的数据封装到Book对象中去
        Book book = new Book();
        try {
            BeanUtils.populate(book,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        System.out.println(book);
        //3.对book中的数据  bookid做判断如果bookid有值 就是修改操作，否则就是新增操作
        Integer i = 0;
        if(book.getBookid()>0){//修改操作
            i = bookDao.updateBook(book);
        }else{//新增操作
            i = bookDao.addBook(book);
        }
        //4.写回数据
        resp.getWriter().write(i+"");
    }
    /**
     *  根据前台传来的参数bookid做删除操作
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void delBookById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.接收前台传递来的参数
        String bookid = req.getParameter("bookid");
        //2.数据操作
        Integer i = bookDao.delBookById(Integer.parseInt(bookid));
        //3.写回数据
        resp.getWriter().write(i+"");
    }

    /**
     * 根据前台传来的bookid和 status来做上下架的操作
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void updateStatus(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.接收前台传递来的参数
        String bookid = req.getParameter("bookid");
        String status = req.getParameter("status");
        //2.数据操作
        Integer i = bookDao.updateStatus(Integer.parseInt(bookid), Integer.parseInt(status));
        System.out.println(i);
        //3.写回数据
        resp.getWriter().write(i+"");
    }
    /**
     * 查询全部书籍的方法
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void findAllBook(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.数据操作
        List<Book> books = bookDao.findAllBook();
        //2.因为layui的table数据表格模块格式是特定的格式，所以我们还要对查到的数据再次封装一下。
        LayTab layTab = new LayTab(books.size(),books);
        //3.把封装好的数据传递到前台页面
        /**
         * 在前后台进行数据交互的时候要以 json格式的字符串进行传递
         * 所以我们还需要把封装好的Laytab再转化成字符串的格式，写回到前端页面
         */
        String s = JSON.toJSONString(layTab);
        //System.out.println("转化后的json格式的字符串laytab数据"+s);
        //4.将转好的字符串发送回前端
        resp.getWriter().write(s);
    }
}
